﻿
/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);

var roomlist=[{name:'test',user:['tom']
			},{name:'test2',user:['jackson']
			 }];

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.post('/login',function(req, res){
  console.log("id : "+req.body.userid +"\npass : "+req.body.userpasswd );
  if(req.body.userpasswd == 'asdf'){
    req.session.username = req.body.userid;
    res.redirect('/list');
  } else {
    req.session.username = null;
    res.redirect('/');
  }
});
app.get('/logout',function(req, res){
  req.session.destroy();
  res.redirect('/');
});

app.get('/list', function(req,res){
  if(req.session.username === undefined || req.session.username === null || req.session.username ==='null'){
    req.session.username = null;
    res.redirect('/');
  } else if(req.session.joinroom === undefined || req.session.joinroom === null){
    res.render('list',{title:"hello", username:req.session.username, rooms:roomlist});
  } else {
    res.redirect('/joingame/'+req.session.joinroom);
  }
});
app.post('/create', function(req, res){
  console.log(roomlist);
  roomlist.push({name:req.body.roomname, user:[req.session.username,'','','']});
  res.redirect('/joingame/'+req.body.roomname);
});
app.get('/joingame/:gamename' , function(req, res){
  console.log(req.params.gamename);
  req.session.joinroom=req.params.gamename;
  res.render('joingame',{title:"a-yo",username:req.session.username,name:req.params.gamename});
});
app.get('/quitgame',function(req,res){
  if(req.session.username !==null){
    if(req.session.joinroom !==null){
	  req.session.joinroom = null;
    }
    res.redirect('/list');
  } else {
    req.session.joinroom = null;
    res.redirect('/');
  }	
});

io.sockets.on('connection', function(socket){
  socket.on('join', function (data){
    console.log(data + "------------11111111111112222222222");
    socket.join(data);
	socket.set('room', data);
  });
  socket.on('message', function (data){
    socket.get('room', function ( error, room) {
      io.sockets.in(room).emit('message', data);
    });
  });
});

server.listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});
